/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.OverdueItem;
import java.util.HashMap;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 *
 * @author Scott Kellish
 */
public class OverdueItemDAO {

    private SqlSessionFactory sqlSessionFactory;

    public OverdueItemDAO() {
        sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
    }

    /**
     * Returns a list of all Item instances from the database
     *
     * @return the list of all Item instances from the database
     */
    public List<OverdueItem> getAll() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            List<OverdueItem> list = session.selectList("OverdueItem.getAll");
            return list;
        } finally {
            session.close();
        }
    }

    /**
     * Returns an OverdueItem instance from the database
     *
     * @param id primary key value used for the lookup
     * @return A Item instance with a primary key value equals to pk, or null
     * if there is no match
     */
    public List<OverdueItem> getByBorrowerId(int id) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            List<OverdueItem> list = session.selectList("OverdueItem.getByBorrowerId", id);
            return list;
        } finally {
            session.close();
        }
    }

    /**
     * Returns an OverdueItem instance from the database
     *
     * @param id primary key value used for the lookup
     * @return A Item instance with a primary key value equals to pk, or null
     * if there is no match
     */
    public OverdueItem getByItemId(int id) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            OverdueItem overdueItem = (OverdueItem) session.selectOne("OverdueItem.getByItemId", id);
            return overdueItem;
        } finally {
            session.close();
        }
    }    
    /**
     * * Updates an instance of Item in the database.
     *
     * @param item the instance to be updated.
     */
    public void update(OverdueItem overdueItem) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.update("OverdueItem.update", overdueItem);
            session.commit();
        } finally {
            session.close();
        }
    }

    /**
     * * Insert an instance of Item into the database.
     *
     * @param item the instance to be persisted.
     */
    public void insert(OverdueItem overdueItem) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.insert("OverdueItem.insert", overdueItem);
            session.commit();
        } finally {
            session.close();
        }
    }

    /**
     * * Delete an instance of Item from the database.
     *
     * @param id primary key value of the instance to be deleted.
     */
    public void deleteBorrowerItem(int borrowerId, int itemId) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            HashMap map = new HashMap();
            map.put("borrowerId", borrowerId);
            map.put("itemId", itemId);
            session.delete("OverdueItem.deleteBorrowerItem", map);
            session.commit();
        } finally {
            session.close();
        }
    }
    
    /**
     * * Delete an instance of Item from the database.
     *
     * @param id primary key value of the instance to be deleted.
     */
    public void deleteByBorrowerId(int borrowerId) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.delete("OverdueItem.deleteByBorrowerId", borrowerId);
            session.commit();
        } finally {
            session.close();
        }
    }
    
    /**
     * Truncates the item table and resets the next rowid() back to 1
     */
    public void truncate() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.delete("OverdueItem.truncate");
            session.commit();
        } finally {
            session.close();
        }        
    }
    
}
